package br.ufrn.engcomp.gui;

import br.ufrn.engcomp.mpc.FThread;
import br.ufrn.engcomp.mpc.TipoControlador;
import br.ufrn.engcomp.mpc.TipoPlanta;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import org.jfree.chart.ChartPanel;

/*
 * TelaPrincipal.java   
 *
 * Created on Mar 19, 2012, 1:49:43 PM
 */
/**
 *
 * @author Victor
 */
public class MainWindow extends javax.swing.JFrame {

    private FThread thread;

    /** Creates new form TelaPrincipal */
    public MainWindow() {
        initComponents();

        thread = new FThread(this);

        entradaPanel.add(new ChartPanel(thread.getChartEscrita()));
        saidaPanel.add(new ChartPanel(thread.getChartLeitura()));

        modifyLookAndFeel();
    }

    public double getSetPoint() {
        return Double.parseDouble(SetPointSpinner.getValue().toString());
    }

    public int getHp() {
        return Integer.parseInt(Hp.getText());
    }

    public int getHu() {
        return Integer.parseInt(Hu.getText());
    }

    public int getHw() {
        return Integer.parseInt(Hw.getText());
    }

    public double[] getQ() {
        return new double[]{Double.parseDouble(Q.getText())};
    }

    public double[] getR() {
        return new double[]{Double.parseDouble(R.getText())};
    }
    
    public double getEntrada() {
        return Double.parseDouble(Entrada.getText());
    }

    public void setTensao(String tensao) {
        this.tensao.setText(tensao);
    }

    public void setNivelT1(String nivelT1) {
        this.nivelT1.setText(nivelT1);
    }

    public void setNivelT2(String nivelT2) {
        this.nivelT2.setText(nivelT2);
    }

    public void setTempoPico(String tempoPico) {
        if (tempoPico == null) {
            this.jLblTempoPico.setText("Tempo de Pico: Indefinido");
        } else {
            this.jLblTempoPico.setText("Tempo de Pico: " + tempoPico + "s");
        }
    }

    public void setSobressinalMaximo(String sobressinalMaximo) {
        if (sobressinalMaximo == null) {
            this.jLblSobressinalMaximo.setText("Sobressinal Máximo: Indefinido");
        } else {
            this.jLblSobressinalMaximo.setText("Sobressinal Máximo: " + sobressinalMaximo + "%");
        }
    }

    public void setTempoAcomodacao(String tempoAcomodacao) {
        if (tempoAcomodacao == null) {
            this.jLblTempoAcomodacao.setText("Tempo de Acomodação: Indefinido");
        } else {
            this.jLblTempoAcomodacao.setText("Tempo de Acomodação: " + tempoAcomodacao + "s");
        }
    }

    private void modifyLookAndFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        connectionPanel = new javax.swing.JPanel();
        jLblTempoPico = new javax.swing.JLabel();
        jLblSobressinalMaximo = new javax.swing.JLabel();
        jLblTempoAcomodacao = new javax.swing.JLabel();
        infoPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tensao = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        nivelT1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        nivelT2 = new javax.swing.JTextField();
        screenshotButton = new javax.swing.JButton();
        chartsPanel = new javax.swing.JPanel();
        entradaPanel = new javax.swing.JPanel();
        saidaPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPnlParametros = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        Hp = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        Hu = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        Hw = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        Q = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        R = new javax.swing.JTextField();
        SetPointSpinner = new javax.swing.JSpinner();
        iniciarButton = new javax.swing.JButton();
        pararButton = new javax.swing.JButton();
        Entrada = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jBtnLevarParaPO = new javax.swing.JButton();
        jPnlControlador = new javax.swing.JPanel();
        jRbnControladorJava = new javax.swing.JRadioButton();
        jRbnControladorHybrid = new javax.swing.JRadioButton();
        jRbnControladorMatlab = new javax.swing.JRadioButton();
        jLabel10 = new javax.swing.JLabel();
        jRbnControladorPID = new javax.swing.JRadioButton();
        jRbnControladorMalhaAberta = new javax.swing.JRadioButton();
        jPnlPlanta = new javax.swing.JSplitPane();
        jPanel3 = new javax.swing.JPanel();
        jLabel12 = new javax.swing.JLabel();
        jRbnModelo = new javax.swing.JRadioButton();
        jRbnSimulador = new javax.swing.JRadioButton();
        jRbnPlantaReal = new javax.swing.JRadioButton();
        jPanel4 = new javax.swing.JPanel();
        jRbnPlantaComRestricoes = new javax.swing.JRadioButton();
        jRbnPlantaSemRestricoes = new javax.swing.JRadioButton();
        jLabel13 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        connectionPanel.setLayout(new java.awt.GridLayout(1, 0));

        jLblTempoPico.setText("Tempo de Pico: Indefinido");
        connectionPanel.add(jLblTempoPico);

        jLblSobressinalMaximo.setText("Sobressinal Máximo: Indefinido");
        connectionPanel.add(jLblSobressinalMaximo);

        jLblTempoAcomodacao.setText("Tempo de Acomodação: Indefinido");
        connectionPanel.add(jLblTempoAcomodacao);

        infoPanel.setLayout(new java.awt.GridLayout(1, 0));

        jLabel1.setText("Tensão (Volts):");
        infoPanel.add(jLabel1);

        tensao.setEditable(false);
        tensao.setText("0");
        infoPanel.add(tensao);

        jLabel2.setText("   Nível T1 (cm): ");
        infoPanel.add(jLabel2);

        nivelT1.setEditable(false);
        nivelT1.setText("0");
        infoPanel.add(nivelT1);

        jLabel3.setText("   Nível T2 (cm): ");
        infoPanel.add(jLabel3);

        nivelT2.setEditable(false);
        nivelT2.setText("0");
        infoPanel.add(nivelT2);

        screenshotButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/ufrn/engcomp/gui/camera.png"))); // NOI18N
        screenshotButton.setToolTipText("Salva screenshot dos gráficos exibidos.");
        screenshotButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                screenshotButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(connectionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
                .addGap(25, 25, 25)
                .addComponent(screenshotButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(infoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(infoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(screenshotButton)
            .addComponent(connectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {connectionPanel, infoPanel});

        chartsPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        chartsPanel.setLayout(new java.awt.GridLayout(2, 0));

        entradaPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Entrada"));
        entradaPanel.setLayout(new java.awt.BorderLayout());
        chartsPanel.add(entradaPanel);

        saidaPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Estados"));
        saidaPanel.setLayout(new java.awt.BorderLayout());
        chartsPanel.add(saidaPanel);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jPnlParametros.setBorder(javax.swing.BorderFactory.createTitledBorder("Parâmetros"));
        jPnlParametros.setPreferredSize(new java.awt.Dimension(525, 147));

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel6.setText("Horizonte de predição (Hp):");

        Hp.setText("5");
        Hp.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                paramFocusGained(evt);
            }
        });
        Hp.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                paramKeyReleased(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel7.setText("Horizonte de controle (Hu):");

        Hu.setText("2");
        Hu.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                paramFocusGained(evt);
            }
        });
        Hu.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                paramKeyReleased(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel8.setText("Horizonte mínimo (Hw):");

        Hw.setText("1");
        Hw.setNextFocusableComponent(Q);
        Hw.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                paramFocusGained(evt);
            }
        });
        Hw.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                paramKeyReleased(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("Set-point (cm):");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel9.setText("Ponderação do erro (Q):");

        Q.setText("1");
        Q.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                paramFocusGained(evt);
            }
        });
        Q.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                paramKeyReleased(evt);
            }
        });

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel11.setText("Ponderação do deltaU (R):");

        R.setText("0.07");
        R.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                paramFocusGained(evt);
            }
        });
        R.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                paramKeyReleased(evt);
            }
        });

        SetPointSpinner.setModel(new javax.swing.SpinnerNumberModel(15.0d, 0.0d, 30.0d, 1.0d));
        ((JSpinner.NumberEditor) SetPointSpinner.getEditor()).getTextField().addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
            public void focusGained(java.awt.event.FocusEvent evt) {
                paramFocusGained(evt);
            }
        });
        ((JSpinner.NumberEditor) SetPointSpinner.getEditor()).getTextField().addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                paramKeyReleased(evt);
            }
        });
        ((JSpinner.NumberEditor) SetPointSpinner.getEditor()).getTextField().setNextFocusableComponent(jBtnLevarParaPO);

        iniciarButton.setText("Iniciar");
        iniciarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                iniciarButtonActionPerformed(evt);
            }
        });

        pararButton.setText("Parar");
        pararButton.setEnabled(false);
        pararButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pararButtonActionPerformed(evt);
            }
        });

        Entrada.setText("0");
        Entrada.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                paramKeyReleased(evt);
            }
        });

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel14.setText("Entrada (MA):");

        jBtnLevarParaPO.setText("PO");
        jBtnLevarParaPO.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnLevarParaPOActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPnlParametrosLayout = new javax.swing.GroupLayout(jPnlParametros);
        jPnlParametros.setLayout(jPnlParametrosLayout);
        jPnlParametrosLayout.setHorizontalGroup(
            jPnlParametrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlParametrosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnlParametrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(Hp, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(Q, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPnlParametrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Hu, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel11)
                    .addComponent(R, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(jPnlParametrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(SetPointSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(Hw, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPnlParametrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPnlParametrosLayout.createSequentialGroup()
                        .addComponent(jLabel14)
                        .addContainerGap())
                    .addGroup(jPnlParametrosLayout.createSequentialGroup()
                        .addComponent(pararButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(iniciarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPnlParametrosLayout.createSequentialGroup()
                        .addComponent(Entrada, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jBtnLevarParaPO))))
        );

        jPnlParametrosLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {R, SetPointSpinner});

        jPnlParametrosLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {iniciarButton, pararButton});

        jPnlParametrosLayout.setVerticalGroup(
            jPnlParametrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlParametrosLayout.createSequentialGroup()
                .addGroup(jPnlParametrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPnlParametrosLayout.createSequentialGroup()
                        .addGap(69, 69, 69)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Q, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPnlParametrosLayout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Hp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPnlParametrosLayout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(jPnlParametrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPnlParametrosLayout.createSequentialGroup()
                                .addComponent(jLabel14)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPnlParametrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(Entrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jBtnLevarParaPO))
                                .addGap(8, 8, 8))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlParametrosLayout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Hw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(SetPointSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlParametrosLayout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Hu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(R, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(37, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlParametrosLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jPnlParametrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(iniciarButton)
                    .addComponent(pararButton)))
        );

        jPnlParametrosLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {R, SetPointSpinner});

        jPnlControlador.setBorder(javax.swing.BorderFactory.createTitledBorder("Controlador"));

        buttonGroup3.add(jRbnControladorJava);
        jRbnControladorJava.setSelected(true);
        jRbnControladorJava.setText("Java");

        buttonGroup3.add(jRbnControladorHybrid);
        jRbnControladorHybrid.setText("Hybrid");

        buttonGroup3.add(jRbnControladorMatlab);
        jRbnControladorMatlab.setText("Matlab");

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel10.setText("Implementação:");

        buttonGroup3.add(jRbnControladorPID);
        jRbnControladorPID.setText("PID");

        buttonGroup3.add(jRbnControladorMalhaAberta);
        jRbnControladorMalhaAberta.setText("Malha Aberta");

        javax.swing.GroupLayout jPnlControladorLayout = new javax.swing.GroupLayout(jPnlControlador);
        jPnlControlador.setLayout(jPnlControladorLayout);
        jPnlControladorLayout.setHorizontalGroup(
            jPnlControladorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlControladorLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnlControladorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRbnControladorPID)
                    .addComponent(jRbnControladorHybrid)
                    .addComponent(jRbnControladorJava)
                    .addComponent(jLabel10)
                    .addComponent(jRbnControladorMatlab)
                    .addComponent(jRbnControladorMalhaAberta))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPnlControladorLayout.setVerticalGroup(
            jPnlControladorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlControladorLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRbnControladorJava)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRbnControladorMatlab)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRbnControladorHybrid)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRbnControladorPID)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 4, Short.MAX_VALUE)
                .addComponent(jRbnControladorMalhaAberta))
        );

        jPnlPlanta.setBorder(javax.swing.BorderFactory.createTitledBorder("Planta"));
        jPnlPlanta.setDividerLocation(121);
        jPnlPlanta.setDividerSize(1);
        jPnlPlanta.setPreferredSize(new java.awt.Dimension(241, 158));

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel12.setText("Tipo:");

        buttonGroup1.add(jRbnModelo);
        jRbnModelo.setText("Modelo");

        buttonGroup1.add(jRbnSimulador);
        jRbnSimulador.setText("Simulador");

        buttonGroup1.add(jRbnPlantaReal);
        jRbnPlantaReal.setSelected(true);
        jRbnPlantaReal.setText("Planta real");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel12)
                    .addComponent(jRbnModelo, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRbnSimulador, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRbnPlantaReal, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRbnModelo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRbnSimulador)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRbnPlantaReal)
                .addContainerGap(50, Short.MAX_VALUE))
        );

        jPnlPlanta.setLeftComponent(jPanel3);

        buttonGroup2.add(jRbnPlantaComRestricoes);
        jRbnPlantaComRestricoes.setSelected(true);
        jRbnPlantaComRestricoes.setText("Com Restrições");

        buttonGroup2.add(jRbnPlantaSemRestricoes);
        jRbnPlantaSemRestricoes.setText("Sem Restrições");

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel13.setText("Restrições:");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRbnPlantaSemRestricoes)
                    .addComponent(jLabel13)
                    .addComponent(jRbnPlantaComRestricoes))
                .addContainerGap(28, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRbnPlantaSemRestricoes)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRbnPlantaComRestricoes)
                .addContainerGap(73, Short.MAX_VALUE))
        );

        jPnlPlanta.setRightComponent(jPanel4);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jPnlPlanta, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPnlControlador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPnlParametros, javax.swing.GroupLayout.DEFAULT_SIZE, 697, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPnlPlanta, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPnlControlador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPnlParametros, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jPnlControlador, jPnlParametros, jPnlPlanta});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(chartsPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chartsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void iniciarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_iniciarButtonActionPerformed
        try {
            String ip = "";
            int port = 0;
            TipoPlanta tipoPlanta = TipoPlanta.Modelo;
            boolean comRestricoes = jRbnPlantaComRestricoes.isSelected();
            TipoControlador tipoControlador = TipoControlador.Java;
    
            if (jRbnSimulador.isSelected()) {
                ip = "127.0.0.1";
                port = 20081;
                tipoPlanta = TipoPlanta.Simulador;
            } else if (jRbnPlantaReal.isSelected()) {
                ip = "10.13.99.69";
                port = 20081;
                tipoPlanta = TipoPlanta.PlantaReal;
            }
            
            if (jRbnControladorMatlab.isSelected()) {
                tipoControlador = TipoControlador.Matlab;
            } else if (jRbnControladorHybrid.isSelected()) {
                tipoControlador = TipoControlador.Hybrid;
            } else if (jRbnControladorMalhaAberta.isSelected()) {
                tipoControlador = TipoControlador.MalhaAberta;
            } else if (jRbnControladorPID.isSelected()) {
                tipoControlador = TipoControlador.PID;
            }
            
            // Ativar execucao da thread
            thread.turnON(tipoPlanta, comRestricoes, tipoControlador, ip, port);

            desabilitarInterface();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Não foi possível estabelecer conexão com o servidor."
                    + "\nVerifique o IP e a porta informados.", "Erro", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_iniciarButtonActionPerformed

    private void pararButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pararButtonActionPerformed
        thread.turnOFF();
        habilitarInterface();
    }//GEN-LAST:event_pararButtonActionPerformed

    private void paramKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_paramKeyReleased
        // TODO add your handling code here:
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            thread.updateControllerParam();
        }
    }//GEN-LAST:event_paramKeyReleased

    private void screenshotButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_screenshotButtonActionPerformed
        thread.takeScreenshot();
    }//GEN-LAST:event_screenshotButtonActionPerformed

    private void jBtnLevarParaPOActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnLevarParaPOActionPerformed
        thread.updateControllerParam(1, 20, 10, new double[] { 1 }, new double[] { 0.5 });
        
        SetPointSpinner.setValue(15);
        
        ((JSpinner.NumberEditor) SetPointSpinner.getEditor()).getTextField().requestFocus();
    }//GEN-LAST:event_jBtnLevarParaPOActionPerformed

    private void paramFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_paramFocusGained
        if (evt.getSource() instanceof JFormattedTextField) { 
            final JFormattedTextField jTxt = (JFormattedTextField) evt.getSource();
            
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    jTxt.selectAll();
                }
            });
        } else if (evt.getSource() instanceof JTextField) {
            JTextField jTxt = (JTextField) evt.getComponent();

            jTxt.selectAll();
        }
    }//GEN-LAST:event_paramFocusGained

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField Entrada;
    private javax.swing.JTextField Hp;
    private javax.swing.JTextField Hu;
    private javax.swing.JTextField Hw;
    private javax.swing.JTextField Q;
    private javax.swing.JTextField R;
    private javax.swing.JSpinner SetPointSpinner;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JPanel chartsPanel;
    private javax.swing.JPanel connectionPanel;
    private javax.swing.JPanel entradaPanel;
    private javax.swing.JPanel infoPanel;
    private javax.swing.JButton iniciarButton;
    private javax.swing.JButton jBtnLevarParaPO;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLblSobressinalMaximo;
    private javax.swing.JLabel jLblTempoAcomodacao;
    private javax.swing.JLabel jLblTempoPico;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPnlControlador;
    private javax.swing.JPanel jPnlParametros;
    private javax.swing.JSplitPane jPnlPlanta;
    private javax.swing.JRadioButton jRbnControladorHybrid;
    private javax.swing.JRadioButton jRbnControladorJava;
    private javax.swing.JRadioButton jRbnControladorMalhaAberta;
    private javax.swing.JRadioButton jRbnControladorMatlab;
    private javax.swing.JRadioButton jRbnControladorPID;
    private javax.swing.JRadioButton jRbnModelo;
    private javax.swing.JRadioButton jRbnPlantaComRestricoes;
    private javax.swing.JRadioButton jRbnPlantaReal;
    private javax.swing.JRadioButton jRbnPlantaSemRestricoes;
    private javax.swing.JRadioButton jRbnSimulador;
    private javax.swing.JTextField nivelT1;
    private javax.swing.JTextField nivelT2;
    private javax.swing.JButton pararButton;
    private javax.swing.JPanel saidaPanel;
    private javax.swing.JButton screenshotButton;
    private javax.swing.JTextField tensao;
    // End of variables declaration//GEN-END:variables

    private void habilitarInterface() {
        jRbnModelo.setEnabled(true);
        jRbnSimulador.setEnabled(true);
        jRbnPlantaReal.setEnabled(true);
        jRbnPlantaSemRestricoes.setEnabled(true);
        jRbnPlantaComRestricoes.setEnabled(true);
        jRbnControladorJava.setEnabled(true);
        jRbnControladorMatlab.setEnabled(true);
        jRbnControladorHybrid.setEnabled(true);
        jRbnControladorPID.setEnabled(true);
        jRbnControladorMalhaAberta.setEnabled(true);
        
        jLabel6.setEnabled(true);
        jLabel7.setEnabled(true);
        jLabel8.setEnabled(true);
        jLabel9.setEnabled(true);
        jLabel11.setEnabled(true);
        jLabel5.setEnabled(true);
        jLabel14.setEnabled(true);
        
        Hp.setEnabled(true);
        Hu.setEnabled(true);
        Hw.setEnabled(true);
        Q.setEnabled(true);
        R.setEnabled(true);
//        SetPointSpinner.setEnabled(true);
        Entrada.setEnabled(true);
        
        iniciarButton.setEnabled(true);
        pararButton.setEnabled(false);
    }
    
    private void desabilitarInterface() {
        jRbnModelo.setEnabled(false);
        jRbnSimulador.setEnabled(false);
        jRbnPlantaReal.setEnabled(false);
        jRbnPlantaSemRestricoes.setEnabled(false);
        jRbnPlantaComRestricoes.setEnabled(false);
        jRbnControladorJava.setEnabled(false);
        jRbnControladorMatlab.setEnabled(false);
        jRbnControladorHybrid.setEnabled(false);
        jRbnControladorPID.setEnabled(false);
        jRbnControladorMalhaAberta.setEnabled(false);
        
        if (jRbnControladorMalhaAberta.isSelected()) {
            jLabel6.setEnabled(false);
            jLabel7.setEnabled(false);
            jLabel8.setEnabled(false);
            jLabel9.setEnabled(false);
            jLabel11.setEnabled(false);
            jLabel5.setEnabled(false);
            jLabel14.setEnabled(true);
            
            Hp.setEnabled(false);
            Hu.setEnabled(false);
            Hw.setEnabled(false);
            Q.setEnabled(false);
            R.setEnabled(false);
//            SetPointSpinner.setEnabled(false);
            Entrada.setEnabled(true);
        } else {
            jLabel6.setEnabled(true);
            jLabel7.setEnabled(true);
            jLabel8.setEnabled(true);
            jLabel9.setEnabled(true);
            jLabel11.setEnabled(true);
            jLabel5.setEnabled(true);
            jLabel14.setEnabled(false);
            
            Hp.setEnabled(true);
            Hu.setEnabled(true);
            Hw.setEnabled(true);
            Q.setEnabled(true);
            R.setEnabled(true);
//            SetPointSpinner.setEnabled(true);
            Entrada.setEnabled(false);
        }
        
        iniciarButton.setEnabled(false);
        pararButton.setEnabled(true);
    }

}
